<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="baidu-site-verification" content="5pQM9dB1uO" />
<meta name="google-site-verification" content="dY4Uxh6geXuXnQR_3mASUgoRCnXG4gmKcISzY0_3vNM" />
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"www.chenkl.vip","root":"/","scheme":"Mist","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":true,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="随着前端的迅猛发展，另外几种前端框架的崛起，jQuery慢慢变得不再是必须。但是我认为，项目中可以不用，但是仍然建议学。">
<meta property="og:type" content="article">
<meta property="og:title" content="前端基础进阶学习(十二)--详细图解jQuery对象，以及如何扩展jQuery插件">
<meta property="og:url" content="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%BA%8C)--%E8%AF%A6%E7%BB%86%E5%9B%BE%E8%A7%A3jQuery%E5%AF%B9%E8%B1%A1%EF%BC%8C%E4%BB%A5%E5%8F%8A%E5%A6%82%E4%BD%95%E6%89%A9%E5%B1%95jQuery%E6%8F%92%E4%BB%B6/index.html">
<meta property="og:site_name" content="小小前端搬运工">
<meta property="og:description" content="随着前端的迅猛发展，另外几种前端框架的崛起，jQuery慢慢变得不再是必须。但是我认为，项目中可以不用，但是仍然建议学。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://sanyuanda.oss-cn-hangzhou.aliyuncs.com/imgs/QQ%E5%9B%BE%E7%89%8720200822100702.png">
<meta property="og:image" content="https://sanyuanda.oss-cn-hangzhou.aliyuncs.com/imgs/QQ%E5%9B%BE%E7%89%8720200822102844.png">
<meta property="article:published_time" content="2020-08-22T09:52:14.000Z">
<meta property="article:modified_time" content="2020-08-23T01:53:06.850Z">
<meta property="article:author" content="Archer">
<meta property="article:tag" content="javaScript">
<meta property="article:tag" content="前端进阶">
<meta property="article:tag" content="jQuery">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://sanyuanda.oss-cn-hangzhou.aliyuncs.com/imgs/QQ%E5%9B%BE%E7%89%8720200822100702.png">

<link rel="canonical" href="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%BA%8C)--%E8%AF%A6%E7%BB%86%E5%9B%BE%E8%A7%A3jQuery%E5%AF%B9%E8%B1%A1%EF%BC%8C%E4%BB%A5%E5%8F%8A%E5%A6%82%E4%BD%95%E6%89%A9%E5%B1%95jQuery%E6%8F%92%E4%BB%B6/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>前端基础进阶学习(十二)--详细图解jQuery对象，以及如何扩展jQuery插件 | 小小前端搬运工</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="小小前端搬运工" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">小小前端搬运工</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">种一棵树最好的时间是十年前，其次就是现在。</h1>
      
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">49</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">7</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">28</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="search-pop-overlay">
  <div class="popup search-popup">
      <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

  </div>
</div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%BA%8C)--%E8%AF%A6%E7%BB%86%E5%9B%BE%E8%A7%A3jQuery%E5%AF%B9%E8%B1%A1%EF%BC%8C%E4%BB%A5%E5%8F%8A%E5%A6%82%E4%BD%95%E6%89%A9%E5%B1%95jQuery%E6%8F%92%E4%BB%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Archer">
      <meta itemprop="description" content="种一棵树最好的时间是十年前，其次就是现在。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小小前端搬运工">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          前端基础进阶学习(十二)--详细图解jQuery对象，以及如何扩展jQuery插件
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-08-22 09:52:14" itemprop="dateCreated datePublished" datetime="2020-08-22T09:52:14Z">2020-08-22</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/javaScript/" itemprop="url" rel="index"><span itemprop="name">javaScript</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>随着前端的迅猛发展，另外几种前端框架的崛起，jQuery慢慢变得不再是必须。但是我认为，项目中可以不用，但是仍然建议学。</p>
</blockquote>
<a id="more"></a>

<h2 id="jQuery对象"><a href="#jQuery对象" class="headerlink" title="jQuery对象"></a>jQuery对象</h2><blockquote>
<p>使用jQuery对象时，我们这样写：</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">// 声明一个jQuery对象</span><br><span class="line">$(<span class="string">'.target'</span>)</span><br><span class="line"></span><br><span class="line">// 获取元素的css属性</span><br><span class="line">$(<span class="string">'.target'</span>).css(<span class="string">'width'</span>)</span><br><span class="line"></span><br><span class="line">// 获取元素的位置信息</span><br><span class="line">$(<span class="string">'.target'</span>).offset()</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在最开始，我们会有许多疑问，比如$是怎么回事？为什么不用new就可以直接声明一个对象？</p>
</blockquote>
<blockquote>
<p>我们直代码来展示并解释</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">;</span><br><span class="line">(<span class="keyword">function</span> (ROOT) &#123;</span><br><span class="line"></span><br><span class="line">  // 构造函数</span><br><span class="line">  var jQuery = <span class="keyword">function</span> (selector) &#123;</span><br><span class="line"></span><br><span class="line">    // 在jQuery中直接返回new过的实例，这里的init是jQuery的真正构造函数</span><br><span class="line">    <span class="built_in">return</span> new jQuery.fn.init(selector)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  jQuery.fn = jQuery.prototype = &#123;</span><br><span class="line">    constructor: jQuery,</span><br><span class="line"></span><br><span class="line">    version: <span class="string">'1.0.0'</span>,</span><br><span class="line"></span><br><span class="line">    init: <span class="keyword">function</span> (selector) &#123;</span><br><span class="line">      // 在jquery中这里有一个复杂的判断，但是这里我做了简化</span><br><span class="line">      var elem, selector;</span><br><span class="line">      elem = document.querySelector(selector);</span><br><span class="line">      this[0] = elem;</span><br><span class="line"></span><br><span class="line">      // 在jquery中返回一个由所有原型属性方法组成的数组，我们这里简化，直接返回this即可</span><br><span class="line">      // <span class="built_in">return</span> jQuery.makeArray(selector, this);</span><br><span class="line">      <span class="built_in">return</span> this;</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    // 在原型上添加一堆方法</span><br><span class="line">    toArray: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    get: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    each: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    ready: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    first: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    slice: <span class="function"><span class="title">function</span></span> () &#123; &#125;</span><br><span class="line">    // ... ...</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  jQuery.fn.init.prototype = jQuery.fn;</span><br><span class="line"></span><br><span class="line">  // 实现jQuery的两种扩展方式</span><br><span class="line">  jQuery.extend = jQuery.fn.extend = <span class="keyword">function</span> (options) &#123;</span><br><span class="line"></span><br><span class="line">    // 在jquery源码中会根据参数不同进行很多判断，我们这里就直接走一种方式，所以就不用判断了</span><br><span class="line">    var target = this;</span><br><span class="line">    var copy;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (name <span class="keyword">in</span> options) &#123;</span><br><span class="line">      copy = options[name];</span><br><span class="line">      target[name] = copy;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">return</span> target;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  // jQuery中利用上面实现的扩展机制，添加了许多方法，其中</span><br><span class="line"></span><br><span class="line">  // 直接添加在构造函数上，被称为工具方法</span><br><span class="line">  jQuery.extend(&#123;</span><br><span class="line">    isFunction: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    <span class="built_in">type</span>: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    parseHTML: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    parseJSON: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    ajax: <span class="function"><span class="title">function</span></span> () &#123; &#125;</span><br><span class="line">    // ...</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  // 添加到原型上</span><br><span class="line">  jQuery.fn.extend(&#123;</span><br><span class="line">    queue: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    promise: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    attr: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    prop: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    addClass: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    removeClass: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    val: <span class="function"><span class="title">function</span></span> () &#123; &#125;,</span><br><span class="line">    css: <span class="function"><span class="title">function</span></span> () &#123; &#125;</span><br><span class="line">    // ...</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  // $符号的由来，实际上它就是jQuery，一个简化的写法，在这里我们还可以替换成其他可用字符</span><br><span class="line">  ROOT.jQuery = ROOT.$ = jQuery;</span><br><span class="line">&#125;)(window);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在上面代码中，我们封装了一个简化版的jQuery对象，它向我们简单展示了jQuery的整体骨架。</p>
</blockquote>
<blockquote>
<p>在代码中可以看到，jQuery自身对于原型的处理使用了一些巧妙的方式，比如<code>jQuery.fn = jQuery.prototype</code>,<code>jQuery.fn.init.prototype = jQuery.fn</code>等，这几句正是jQuery对象的关键所在。</p>
</blockquote>
<p><img src="https://sanyuanda.oss-cn-hangzhou.aliyuncs.com/imgs/QQ%E5%9B%BE%E7%89%8720200822100702.png" alt=""></p>
<h2 id="对象封装分析"><a href="#对象封装分析" class="headerlink" title="对象封装分析"></a>对象封装分析</h2><blockquote>
<p>在上面的实现中，首先在jQuery构造函数里声明了一个fn属性，并将其指向了原型<code>jQuery.prototype</code>。然后在原型中添加了init方法。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jQuyer.fn = jQuery.prototype = &#123;</span><br><span class="line">  init: <span class="function"><span class="title">function</span></span>() &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>之后又将init的原型，指向了jQuery.prototype</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jQuery.fn.init.prototype = jQuery.fn</span><br></pre></td></tr></table></figure>

<blockquote>
<p>而在构造函数jQuery，返回了init的实例对象</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var jQuery = <span class="keyword">function</span>(selector) &#123;</span><br><span class="line">  // 在jQuery中直接返回new过的实例，这里的init是jQuery的真正构造函数</span><br><span class="line">  <span class="built_in">return</span> new jQuery.fn.init(selector)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>最后对外暴露入口时，将字符<code>$</code>与<code>jQuery</code>对等起来</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ROOT.jQuery = ROOT.$ = jQuery</span><br></pre></td></tr></table></figure>

<blockquote>
<p>因此当我们直接使用<code>$(&#39;#test&#39;)</code>创建一个对象时，实际上是创建了一个init的实例，这里的真正构造函数时原型中的init方法。</p>
</blockquote>
<div class="note warning">
            <p>许多对jQuery内部实现不太了解的人，常常会毫无节制使用<code>$()</code>，比如对于同一个元素的不同操作。</p>
          </div>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var width = parseInt($(<span class="string">'#test'</span>).css(<span class="string">'width'</span>))</span><br><span class="line"><span class="keyword">if</span> (width &gt; 20) &#123;</span><br><span class="line">  $(<span class="string">'#test'</span>).css(<span class="string">'backgroundColor'</span>, <span class="string">'red'</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>通过我们上面的分析，我们知道当我们执行<code>$()</code>时，就会重新生成一个init实例对象，因此当我们这样没有节制的使用jQuery是非常不正确的，虽然看上去方便一些，但是对于内存的消耗非常大。正确的做法是既然是同一个对象，那么就用一个变量保存起来后续使用即可。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var <span class="variable">$test</span> = $(<span class="string">'#test'</span>)</span><br><span class="line">var width = parseInt(<span class="variable">$test</span>.css(<span class="string">'width'</span>))</span><br><span class="line"><span class="keyword">if</span> (width &gt; 20) &#123;</span><br><span class="line">  <span class="variable">$test</span>.css(<span class="string">'backgroundColor'</span>, <span class="string">'red'</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="扩展方法分析"><a href="#扩展方法分析" class="headerlink" title="扩展方法分析"></a>扩展方法分析</h2><blockquote>
<p>在上面的代码中，我们还简单实现了两个扩展方法</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">jQuery.extend = jQuery.fn.extend = <span class="keyword">function</span> (options) &#123;</span><br><span class="line"></span><br><span class="line">  // 在jquery源码中会根据参数不同进行很多判断，我们这里就直接走一种方式，所以就不用判断了</span><br><span class="line">  var target = this;</span><br><span class="line">  var copy;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (name <span class="keyword">in</span> options) &#123;</span><br><span class="line">    copy = options[name];</span><br><span class="line">    target[name] = copy;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">return</span> target;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>要理解它的实现，首先要明确知道this指向。</p>
</blockquote>
<blockquote>
<p>传入的参数options为一个<code>key: value</code>模式的对象，我们通过<code>for in</code>遍历options，将key作为jQuery的新属性，value作为该新属性所对应的新方法，分别添加到jQuery方法和jQuery.fn中。</p>
</blockquote>
<blockquote>
<p>也就是说，当我们通过<code>jQuery.extend</code>扩展jQuery时，方法被添加到了jQuery构造函数中，而当我们通过<code>jQuery.fn.extend扩展jQuery</code>时，方法被添加到了jQuery原型中。</p>
</blockquote>
<h2 id="被静态方法，工具方法和实例方法"><a href="#被静态方法，工具方法和实例方法" class="headerlink" title="被静态方法，工具方法和实例方法"></a>被静态方法，工具方法和实例方法</h2><blockquote>
<p>关于封装一个对象。我们之前提到过，在封装对象时，属性和方法可以具体放置的三个位置，并且对于这三个位置的不同做了一个详细的解读。</p>
</blockquote>
<p><img src="https://sanyuanda.oss-cn-hangzhou.aliyuncs.com/imgs/QQ%E5%9B%BE%E7%89%8720200822102844.png" alt=""></p>
<blockquote>
<p>在实现jQuery扩展方法时，一部分方法需要扩展到构造函数中，一部分方法需要扩展到原型中，当我们通读jQuery源码时，还发现有一些方法在了模块作用域中。</p>
</blockquote>
<blockquote>
<p>这里用一个例子简单区分一下。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">// 模块内部</span><br><span class="line">const a = 20</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> Person(name, age) &#123;</span><br><span class="line">  this.name = name</span><br><span class="line">  this.age = age</span><br><span class="line">  // 构造函数方法，每声明一个实例，都会重新创建一次，属于实例独有</span><br><span class="line">  this.getName = <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">    <span class="built_in">return</span> this.name</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 原型方法，仅在原型创建时声明一次，属于所有实例共享</span><br><span class="line">Person.prototype.getAge = <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">  <span class="built_in">return</span> this.age</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 工具方法，直接挂载在构造函数名上，仅声明一次，无法直接访问实例内部属性与方法</span><br><span class="line">Person.each = <span class="function"><span class="title">function</span></span>() &#123;&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>如上，each就是一个工具方法，或者说静态方法。</p>
</blockquote>
<blockquote>
<p>工具方法的特性也和工具一词非常贴近，他们与实例的自身属性毫无关联，仅仅只是实现一些通用的功能，我们可以通过<code>$.each</code>与<code>$(&#39;div&#39;).each</code>这2个方法来体会工具方法与实例方法的不同之处。</p>
</blockquote>
<blockquote>
<p>在实际开发中，我们运用到非常多的一个工具库就是<code>lodash.js</code></p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$.ajax()</span><br><span class="line">$.isFunction()</span><br><span class="line">$.each()</span><br><span class="line">... ...</span><br></pre></td></tr></table></figure>

<blockquote>
<p>放在原型中的方法，在使用时必须创建一个新的实例对象才能访问，因此这样的方法叫做实例方法。也正因为这一点，他的使用成本会比工具方法高一些。但是相比构造函数方法，原型方法更节省内存。</p>
</blockquote>
<blockquote>
<p>所以通过<code>$.extend</code>扩展的方法，其实就是对工具方法的扩展，而通过<code>$.fn.extend</code>扩展的方法，就是对于实例方法的扩展。</p>
</blockquote>
<h2 id="jQuery插件的实现"><a href="#jQuery插件的实现" class="headerlink" title="jQuery插件的实现"></a>jQuery插件的实现</h2><blockquote>
<p>具体也没有什么可以多说的了，大家看了代码就可以明白一切。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">  &lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"utf-8"</span>&gt;</span><br><span class="line">    &lt;title&gt;&lt;/title&gt;</span><br><span class="line">    &lt;style <span class="built_in">type</span>=<span class="string">"text/css"</span>&gt;</span><br><span class="line">      <span class="comment">#target,</span></span><br><span class="line">      <span class="comment">#target2 &#123;</span></span><br><span class="line">        width: 50px;</span><br><span class="line">        height: 50px;</span><br><span class="line">        background-color: orange;</span><br><span class="line">        cursor: move;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="comment">#target2 &#123;</span></span><br><span class="line">        background-color: red;</span><br><span class="line">      &#125;</span><br><span class="line">    &lt;/style&gt;</span><br><span class="line">  &lt;/head&gt;</span><br><span class="line">  &lt;body&gt;</span><br><span class="line">    &lt;div id=<span class="string">"target"</span>&gt;&lt;/div&gt;</span><br><span class="line">    &lt;div id=<span class="string">"target2"</span>&gt;&lt;/div&gt;</span><br><span class="line">  &lt;/body&gt;</span><br><span class="line">  &lt;script src=<span class="string">"http://libs.baidu.com/jquery/2.1.4/jquery.min.js"</span>&gt;&lt;/script&gt;</span><br><span class="line">  &lt;script <span class="built_in">type</span>=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">    ;</span><br><span class="line">    (<span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">      // 这是一个私有属性，不需要被实例访问</span><br><span class="line">      var transform = getTransform();</span><br><span class="line"></span><br><span class="line">      <span class="keyword">function</span> Drag(selector) &#123;</span><br><span class="line">        // 放在构造函数中的属性，都是属于每一个实例单独拥有</span><br><span class="line">        this.elem = typeof selector == <span class="string">'Object'</span> ? selector : document.getElementById(selector);</span><br><span class="line">        this.startX = 0;</span><br><span class="line">        this.startY = 0;</span><br><span class="line">        this.sourceX = 0;</span><br><span class="line">        this.sourceY = 0;</span><br><span class="line"></span><br><span class="line">        this.init();</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">      // 原型</span><br><span class="line">      Drag.prototype = &#123;</span><br><span class="line">        constructor: Drag,</span><br><span class="line"></span><br><span class="line">        init: <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">          // 初始时需要做些什么事情</span><br><span class="line">          this.setDrag();</span><br><span class="line">        &#125;,</span><br><span class="line"></span><br><span class="line">        // 稍作改造，仅用于获取当前元素的属性，类似于getName</span><br><span class="line">        getStyle: <span class="keyword">function</span>(property) &#123;</span><br><span class="line">          <span class="built_in">return</span> document.defaultView.getComputedStyle ? document.defaultView.getComputedStyle(this.elem, <span class="literal">false</span>)[</span><br><span class="line">            property] : this.elem.currentStyle[property];</span><br><span class="line">        &#125;,</span><br><span class="line"></span><br><span class="line">        // 用来获取当前元素的位置信息，注意与之前的不同之处</span><br><span class="line">        getPosition: <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">          var pos = &#123;</span><br><span class="line">            x: 0,</span><br><span class="line">            y: 0</span><br><span class="line">          &#125;;</span><br><span class="line">          <span class="keyword">if</span> (transform) &#123;</span><br><span class="line">            var transformValue = this.getStyle(transform);</span><br><span class="line">            <span class="keyword">if</span> (transformValue == <span class="string">'none'</span>) &#123;</span><br><span class="line">              this.elem.style[transform] = <span class="string">'translate(0, 0)'</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">              var temp = transformValue.match(/-?\d+/g);</span><br><span class="line">              pos = &#123;</span><br><span class="line">                x: parseInt(temp[4].trim()),</span><br><span class="line">                y: parseInt(temp[5].trim())</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (this.getStyle(<span class="string">'position'</span>) == <span class="string">'static'</span>) &#123;</span><br><span class="line">              this.elem.style.position = <span class="string">'relative'</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">              pos = &#123;</span><br><span class="line">                x: parseInt(this.getStyle(<span class="string">'left'</span>) ? this.getStyle(<span class="string">'left'</span>) : 0),</span><br><span class="line">                y: parseInt(this.getStyle(<span class="string">'top'</span>) ? this.getStyle(<span class="string">'top'</span>) : 0)</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line"></span><br><span class="line">          <span class="built_in">return</span> pos;</span><br><span class="line">        &#125;,</span><br><span class="line"></span><br><span class="line">        // 用来设置当前元素的位置</span><br><span class="line">        setPostion: <span class="keyword">function</span>(pos) &#123;</span><br><span class="line">          <span class="keyword">if</span> (transform) &#123;</span><br><span class="line">            this.elem.style[transform] = <span class="string">'translate('</span> + pos.x + <span class="string">'px, '</span> + pos.y + <span class="string">'px)'</span>;</span><br><span class="line">          &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            this.elem.style.left = pos.x + <span class="string">'px'</span>;</span><br><span class="line">            this.elem.style.top = pos.y + <span class="string">'px'</span>;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;,</span><br><span class="line"></span><br><span class="line">        // 该方法用来绑定事件</span><br><span class="line">        setDrag: <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">          var self = this;</span><br><span class="line">          this.elem.addEventListener(<span class="string">'mousedown'</span>, start, <span class="literal">false</span>);</span><br><span class="line"></span><br><span class="line">          <span class="keyword">function</span> start(event) &#123;</span><br><span class="line">            self.startX = event.pageX;</span><br><span class="line">            self.startY = event.pageY;</span><br><span class="line"></span><br><span class="line">            var pos = self.getPosition();</span><br><span class="line"></span><br><span class="line">            self.sourceX = pos.x;</span><br><span class="line">            self.sourceY = pos.y;</span><br><span class="line"></span><br><span class="line">            document.addEventListener(<span class="string">'mousemove'</span>, move, <span class="literal">false</span>);</span><br><span class="line">            document.addEventListener(<span class="string">'mouseup'</span>, end, <span class="literal">false</span>);</span><br><span class="line">          &#125;</span><br><span class="line"></span><br><span class="line">          <span class="keyword">function</span> move(event) &#123;</span><br><span class="line">            var currentX = event.pageX;</span><br><span class="line">            var currentY = event.pageY;</span><br><span class="line"></span><br><span class="line">            var distanceX = currentX - self.startX;</span><br><span class="line">            var distanceY = currentY - self.startY;</span><br><span class="line"></span><br><span class="line">            self.setPostion(&#123;</span><br><span class="line">              x: (self.sourceX + distanceX).toFixed(),</span><br><span class="line">              y: (self.sourceY + distanceY).toFixed()</span><br><span class="line">            &#125;)</span><br><span class="line">          &#125;</span><br><span class="line"></span><br><span class="line">          <span class="keyword">function</span> end(event) &#123;</span><br><span class="line">            document.removeEventListener(<span class="string">'mousemove'</span>, move);</span><br><span class="line">            document.removeEventListener(<span class="string">'mouseup'</span>, end);</span><br><span class="line">            // <span class="keyword">do</span> other things</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      // 私有方法，仅仅用来获取transform的兼容写法</span><br><span class="line">      <span class="keyword">function</span> <span class="function"><span class="title">getTransform</span></span>() &#123;</span><br><span class="line">        var transform = <span class="string">''</span>,</span><br><span class="line">          divStyle = document.createElement(<span class="string">'div'</span>).style,</span><br><span class="line">          transformArr = [<span class="string">'transform'</span>, <span class="string">'webkitTransform'</span>, <span class="string">'MozTransform'</span>, <span class="string">'msTransform'</span>, <span class="string">'OTransform'</span>],</span><br><span class="line"></span><br><span class="line">          i = 0,</span><br><span class="line">          len = transformArr.length;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (; i &lt; len; i++) &#123;</span><br><span class="line">          <span class="keyword">if</span> (transformArr[i] <span class="keyword">in</span> divStyle) &#123;</span><br><span class="line">            <span class="built_in">return</span> transform = transformArr[i];</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">return</span> transform;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      // 一种对外暴露的方式</span><br><span class="line">      window.Drag = Drag;</span><br><span class="line">    &#125;)();</span><br><span class="line"></span><br><span class="line">    // 通过扩展方法将拖拽扩展为jQuery的一个实例方法</span><br><span class="line">    (<span class="keyword">function</span> ($) &#123;</span><br><span class="line">      $.fn.extend(&#123;</span><br><span class="line">        becomeDrag: <span class="keyword">function</span> (selector) &#123;</span><br><span class="line">          new Drag(selector);</span><br><span class="line">          <span class="built_in">return</span> this;   // 注意：为了保证jQuery所有的方法都能够链式访问，每一个方法的最后都需要返回this，即返回jQuery实例</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;)(jQuery);</span><br><span class="line">    </span><br><span class="line">    // 使用</span><br><span class="line">    $.fn.becomeDrag(<span class="string">'target'</span>);</span><br><span class="line">    $.fn.becomeDrag(<span class="string">'target2'</span>);</span><br><span class="line">  &lt;/script&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
    </div>

    <div>
      
        <div>
  
    <div style="text-align:center;color: #ccc;font-size:20px;">------------- 本 文 结 束 <i class="fa fa-paw"></i> 感 谢 您 的 阅 读 -------------</div>
  
</div>
      
    </div>
    
    
    
    
        <div class="reward-container">
  <div>只是为了好玩罢了~</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.jpg" alt="Archer 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="Archer 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Archer
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%BA%8C)--%E8%AF%A6%E7%BB%86%E5%9B%BE%E8%A7%A3jQuery%E5%AF%B9%E8%B1%A1%EF%BC%8C%E4%BB%A5%E5%8F%8A%E5%A6%82%E4%BD%95%E6%89%A9%E5%B1%95jQuery%E6%8F%92%E4%BB%B6/" title="前端基础进阶学习(十二)--详细图解jQuery对象，以及如何扩展jQuery插件">https://www.chenkl.vip/前端基础进阶学习(十二)--详细图解jQuery对象，以及如何扩展jQuery插件/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/javaScript/" rel="tag"><i class="fa fa-tag"></i> javaScript</a>
              <a href="/tags/%E5%89%8D%E7%AB%AF%E8%BF%9B%E9%98%B6/" rel="tag"><i class="fa fa-tag"></i> 前端进阶</a>
              <a href="/tags/jQuery/" rel="tag"><i class="fa fa-tag"></i> jQuery</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%B8%80)--%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E5%AE%9E%E6%88%98%E4%B9%8B%E5%B0%81%E8%A3%85%E6%8B%96%E6%8B%BD%E5%AF%B9%E8%B1%A1/" rel="prev" title="前端基础进阶学习(十一)--面向对象实战之封装拖拽对象">
      <i class="fa fa-chevron-left"></i> 前端基础进阶学习(十一)--面向对象实战之封装拖拽对象
    </a></div>
      <div class="post-nav-item">
    <a href="/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%B8%89)--%E6%B7%B1%E5%85%A5%E6%A0%B8%E5%BF%83%EF%BC%8C%E8%AF%A6%E8%A7%A3%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/" rel="next" title="前端基础进阶学习(十三)--深入核心，详解事件循环机制">
      前端基础进阶学习(十三)--深入核心，详解事件循环机制 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#jQuery对象"><span class="nav-number">1.</span> <span class="nav-text">jQuery对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#对象封装分析"><span class="nav-number">2.</span> <span class="nav-text">对象封装分析</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#扩展方法分析"><span class="nav-number">3.</span> <span class="nav-text">扩展方法分析</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#被静态方法，工具方法和实例方法"><span class="nav-number">4.</span> <span class="nav-text">被静态方法，工具方法和实例方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#jQuery插件的实现"><span class="nav-number">5.</span> <span class="nav-text">jQuery插件的实现</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Archer"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Archer</p>
  <div class="site-description" itemprop="description">种一棵树最好的时间是十年前，其次就是现在。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">28</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">49</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/kailong321200875" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;kailong321200875" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:502431556@qq.com" title="E-Mail → mailto:502431556@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/meng-shou-kong-cheng-43/activities" title="知乎 → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;meng-shou-kong-cheng-43&#x2F;activities" rel="noopener" target="_blank"><i class="fa fa-fw fa-ioxhost"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-fas fa-heartbeat"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Archer</span>
  <span class="post-meta-divider">|</span>
  <span class="post-meta-item-icon">
    <i class="fa fa-area-chart"></i>
  </span>
  <span class="post-count">站点总字数：43.4k字</span>
</div>
  
  
  

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      访客数<span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>人次
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      总访问量<span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>次
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : 'e6f2f10abe388d591867',
      clientSecret: '580aea9e3da136ac529a71232367d1fdf3591926',
      repo        : 'blogTalk',
      owner       : 'kailong321200875',
      admin       : ['kailong321200875'],
      id          : '3bbe4461c0a1e22c0ea5fdd4f35ce7b0',
        language: 'zh-CN',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

</body>
</html>

<!-- 页面鼠标点击样式 -->

  
    <script src="/js/src/fireworks.js"></script>
  





  <script src="https://cdn.jsdelivr.net/gh/kailong321200875/live2d-widget@1.0.1/autoload.js"></script>

